package com.qzsoft.zhjd.materiel.modules.materieloperator.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.FunctionUtils;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materieloperator.dto.MaterielOperatorListDto;
import com.qzsoft.zhjd.materiel.modules.materieloperator.entity.MaterielOperatorEntity;
import com.qzsoft.zhjd.materiel.modules.materieloperator.mapper.MaterielOperatorMapper;
import com.qzsoft.zhjd.materiel.modules.materieloperator.service.MaterielOperatorService;

/**
 * @ClassName MaterielOperatorServiceImpl
 * @Description 地磅操作员业务接口实现
 * @author linhuawei
 * @since 2022-03-31
 */
@Service
public class MaterielOperatorServiceImpl extends ServiceImpl<MaterielOperatorMapper, MaterielOperatorEntity>
        implements MaterielOperatorService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

//    @Autowired
//    private MaterielApiService materielApiService;
//
//    @Autowired
//    private MaterielOperatorService materielOperatorService;
//
//    @Autowired
//    private MaterielApiVesionService materielApiVesionService;
//
//
//    @Value("${zhjd.upload.getOperatorInfo}")
//    private String getOperatorInfo;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielOperatorListDto> pageDto(Map<String, Object> params) {
        IPage<MaterielOperatorListDto> page = baseServiceComponent.getPage(params, MaterielOperatorListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    @ZhjdDataFilter
    public List<MaterielOperatorEntity> list(Map<String, Object> params) {
        QueryWrapper<MaterielOperatorEntity> queryWrapper = new QueryWrapper<MaterielOperatorEntity>();
        if (ObjectUtils.isNotEmpty(params.get("baseCode"))) {
            queryWrapper.eq("base_code", params.get("baseCode").toString());
        }
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            queryWrapper.apply(params.get(ZhjdDataFilter.DATA_FILTER_SQL).toString());
        }
        queryWrapper.orderByAsc("base_code, name");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<MaterielOperatorEntity> getByBaseCode(String baseCode) {
        QueryWrapper<MaterielOperatorEntity> queryWrapper = new QueryWrapper<MaterielOperatorEntity>();
        queryWrapper.eq("base_code", baseCode);
        queryWrapper.orderByAsc("base_code, name");
        return baseMapper.selectList(queryWrapper);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void getUrlInfo() throws IOException {
//        //获取访问接口的url
//        String url = getOperatorInfo;
//        Map<String,Object> params = new HashMap<String,Object>();
//        Long getOperators = materielApiVesionService.getVersionByCode("getOperators");
//        params.put("getOperators",String.valueOf(getOperators));
//        //发送请求
//        String result = HttpUtil.get(url,params);
//        JSONObject resultJson = JSONObject.parseObject(result);
//        if(resultJson == null){
//            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "传输数据格式不正确");
//        }
//        Integer status = resultJson.getInteger("code");
//        if(status != 200){
//            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "请求失败");
//        }
//        JSONArray datas = resultJson.getJSONArray("data");
//        if(datas == null){
//            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "返回数据格式异常");
//        }
//        //获取过磅操作员数据
//        List<RequestMaterielOperatorDto> operators = datas.toJavaList(RequestMaterielOperatorDto.class);
//        //更新version
//        materielApiVesionService.updateByCode("getOperators");
//        //删除数据
//        materielOperatorService.remove(null);
//        //处理响应数据，存储过磅操作员信息
//        for(RequestMaterielOperatorDto operator : operators){
//            MaterielOperatorEntity materielOperatorEntity = new MaterielOperatorEntity();
//            materielOperatorEntity.setName(operator.getName());
//            materielOperatorEntity.setAccount(operator.getAccount());
//            materielOperatorEntity.setPassword(operator.getPassword());
//            materielOperatorEntity.setTellphone(operator.getTellphone());
//            materielOperatorEntity.setRemark(operator.getRemark());
//            materielOperatorEntity.setEnable(operator.getEnable());
//            materielOperatorService.save(materielOperatorEntity);
//        }
//    }

    @Override
    public boolean save(MaterielOperatorEntity entity) {
        // 判断账号是否重复
        QueryWrapper<MaterielOperatorEntity> queryWrapper = new QueryWrapper<MaterielOperatorEntity>();
        queryWrapper.eq("account", entity.getAccount());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "账号[" + entity.getAccount() + "]已经存在");
        }

        // 密码加密
        String password = entity.getAccount() + entity.getPassword();
        password = FunctionUtils.md5L32(password);
        entity.setPassword(password);

        // 执行添加操作
        return super.save(entity);
    }

    @Override
    public boolean updateById(MaterielOperatorEntity entity) {
        // 判断用户是否存在
        MaterielOperatorEntity temp = baseMapper.selectById(entity.getId());
        if (null == temp) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "操作员信息不存在");
        }

        // 判断是否修改密码
        if (ObjectUtils.isNotEmpty(entity.getPassword())) {
            String password = temp.getAccount() + entity.getPassword();
            password = FunctionUtils.md5L32(password);
            entity.setPassword(password);
        }
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        // TODO 判断是否需要删除
        return super.removeById(id);
    }

}
